<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { MessagePlugin } from 'tdesign-vue-next'

const router = useRouter()
const userStore = useUserStore()

// 登录表单数据
const formData = reactive({
  username: '',
  password: '',
  remember: false
})

// 表单规则
const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

// 加载状态
const loading = ref(false)

// 表单实例
const formRef = ref(null)

// 处理登录
const handleLogin = async () => {
  // 表单验证
  const valid = await formRef.value.validate()
  if (!valid) return

  try {
    loading.value = true
    
    // 调用登录接口
    const result = await userStore.login({
      username: formData.username,
      password: formData.password
    })
    
    if (result.success) {
      MessagePlugin.success('登录成功')
      router.push('/')
    } else {
      MessagePlugin.error(result.message || '登录失败')
    }
  } catch (error) {
    console.error('登录异常:', error)
    MessagePlugin.error('登录异常，请稍后重试')
  } finally {
    loading.value = false
  }
}
</script>

<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <img src="/vite.svg" class="logo" alt="Logo" />
        <h1 class="title">贵州四季康养管理系统</h1>
      </div>
      
      <t-form
        ref="formRef"
        :data="formData"
        :rules="rules"
        class="login-form"
        @submit="handleLogin"
      >
        <t-form-item name="username">
          <t-input
            v-model="formData.username"
            placeholder="请输入用户名"
            size="large"
            clearable
          >
            <template #prefix-icon>
              <t-icon name="user" />
            </template>
          </t-input>
        </t-form-item>
        
        <t-form-item name="password">
          <t-input
            v-model="formData.password"
            type="password"
            placeholder="请输入密码"
            size="large"
            clearable
          >
            <template #prefix-icon>
              <t-icon name="lock-on" />
            </template>
          </t-input>
        </t-form-item>
        
        <t-form-item>
          <div class="remember-forgot">
            <t-checkbox v-model="formData.remember">记住我</t-checkbox>
            <t-link theme="primary" hover="color">忘记密码？</t-link>
          </div>
        </t-form-item>
        
        <t-form-item>
          <t-button
            type="submit"
            theme="primary"
            size="large"
            block
            :loading="loading"
          >
            登录
          </t-button>
        </t-form-item>
      </t-form>
      
      <div class="login-footer">
        <p>测试账号: admin / 密码: admin123</p>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
  background-image: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.login-card {
  width: 400px;
  padding: 40px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 30px;
}

.logo {
  width: 64px;
  height: 64px;
  margin-bottom: 16px;
}

.title {
  font-size: 24px;
  color: #333;
  margin: 0;
}

.login-form {
  margin-bottom: 20px;
}

.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.login-footer {
  text-align: center;
  color: #999;
  font-size: 14px;
}
</style> 